import { isNil } from 'lodash-es';

import type { FormSchema } from '@/components/form';

interface UseFormValuesContext {
  defaultFormModel: Ref<Recordable>;
  getSchema: ComputedRef<FormSchema[]>;
  formModel: Recordable;
}

export function useFormValues({ defaultFormModel, getSchema, formModel }: UseFormValuesContext) {
  /**
   * 初始化表单
   */
  function initDefault() {
    const schemas = unref(getSchema);
    const obj: Recordable = {};
    schemas.forEach(item => {
      const { defaultValue } = item;
      if (!isNil(defaultValue)) {
        obj[item.field] = defaultValue;
        formModel[item.field] = defaultValue;
      }
    });
    defaultFormModel.value = obj;
  }

  return {
    initDefault,
  };
}
